﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PIM_ATE
{
    public partial class FrmCalConfirmPimManual : Form
    {
        List<Electrical.ClsCalRecordItem> m_lstCheckRecordItem;
        Electrical.ClsCalRecord m_CheckRecord;
        ClsDataUser m_usrObj;
        DataTable m_rcuconfig;
        public FrmCalConfirmPimManual(ClsDataUser usrObj)
        {
            InitializeComponent();
            m_usrObj = usrObj;
            creatCheckRecord(true);
        }

        List<string> getColNames()
        {
            List<string> names = new List<string>();
            names.Add("校准端口号");
            names.Add("校准频段");
            names.Add("校准指标");
            names.Add("校准值");
            names.Add("校准结果");

            return names;
        }
        DataTable toTable(List<Electrical.ClsCalRecordItem> lstItem)
        {
            List<string> cols = getColNames();
            DataTable dt = new DataTable();
            for (int i = 0; i < cols.Count; i++)
            {
                dt.Columns.Add(cols[i]);
            }

            for (int n = 0; n < lstItem.Count; n++)
            {
                int i = 0;
                DataRow dr = dt.NewRow();
                dr[i++] = lstItem[n].Port;
                dr[i++] = lstItem[n].Band;
                dr[i++] = lstItem[n].Spec;
                dr[i++] = lstItem[n].Value;
                dr[i++] = lstItem[n].IsPass;
                dt.Rows.Add(dr);
            }
            return dt;
        }

        void creatCheckRecord(bool isCreat = true)
        {
            if (isCreat)
            {
                m_CheckRecord = new Electrical.ClsCalRecord();
                m_lstCheckRecordItem = new List<Electrical.ClsCalRecordItem>();
                m_CheckRecord.PCIP = m_usrObj.sEquID;
                m_CheckRecord.UserID = m_usrObj.sUser;
                m_CheckRecord.UserID2 = m_usrObj.sUser2;
                m_CheckRecord.ValueType = "交调";
            }
            else
            {
                m_CheckRecord = null;
                m_lstCheckRecordItem = null;
            }
        }
        void updateCalRecord()
        {
            Electrical.ClsDataCalResult CheckResult = new Electrical.ClsDataCalResult(GDefine.DBNameELEC);
            if (CheckResult.updateRecord(m_CheckRecord, m_lstCheckRecordItem))
            {
                MessageBox.Show("上传校准数据成功");
            }
            else
            {
                MessageBox.Show("上传校准数据失败");
            }
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            if (txt_port.Text.Trim() == "")
            {
                MessageBox.Show("端口号不能为空！"); return;
            }
            if (txt_val.Text.Trim() == "")
            {
                MessageBox.Show("校准值不能为空！"); return;
            }
            if (txt_band.Text.Trim() == "")
            {
                MessageBox.Show("校准频段不能为空！"); return;
            }

            Electrical.ClsCalRecordItem item = new Electrical.ClsCalRecordItem();
            item.Port = item.PortIndex = txt_port.Text.Trim();
            item.Spec = "[-INF," + nud_Spec.Value.ToString() + "]";
            item.Value = txt_val.Text.Trim();
            item.Band = txt_band.Text.Trim();
            item.ValueType = "PIM";
            try
            {
             
                float fval = float.Parse(item.Value);
                //float fspec = float.Parse(item.Spec);
                item.IsPass = PIMFunction.compareSpec(fval, item.Spec) ? GDefine.ContentPass : GDefine.ContentFail;
                //item.IsPass = fval > fspec ? GDefine.ContentFail : GDefine.ContentPass;
                //item.Spec = "≤" + item.Spec;
                m_lstCheckRecordItem.Add(item);

                m_rcuconfig = toTable(m_lstCheckRecordItem);
                dataGridView1.DataSource = m_rcuconfig;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }



        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            if (txt_idn.Text.Trim() == "")
            {
                MessageBox.Show("序列号不能为空！"); return;
            }
            m_CheckRecord.InstrumentID = txt_idn.Text.Trim();
            updateCalRecord();
        }

        private void btn_clear_Click(object sender, EventArgs e)
        {
            creatCheckRecord(true);
            m_rcuconfig = toTable(m_lstCheckRecordItem);
            dataGridView1.DataSource = m_rcuconfig;
        }
    }
}
